﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DelonixRegiaHMSys.Core.Models;
using DelonixRegiaHMSys.Core.Interfaces;
using DelonixRegiaHMSys.Infrastructure.Data;

namespace DelonixRegiaHMSys.Infrastructure.Data
{
    public class EfRoomQuery : IRoomQuery
    {
        public IEnumerable<RoomDetails> GetAllRoomNum()
        {
            var context = new hmsDBEntities();
            return context.roomDetails
                .Select(r => new RoomDetails()
                {
                    RoomNum = r.roomNum,
                    FloorLevel = r.floorLevel,
                    RoomType = r.roomType,
                    BedType = r.bedType,
                    SmokingOrNot = r.smokingOrNot,
                    MaxOccupancy = r.maxOccupancy,
                    RoomStatus = r.roomStatus,
                    RoomRates = r.roomRates,
                    GuestID = r.guestID
                });
        }

        public RoomDetails GetRoomByNum(int roomNum)
        {
            var context = new hmsDBEntities();
            var userToRetrieve = context.roomDetails.FirstOrDefault(r => r.roomNum == roomNum);
            RoomDetails a = new RoomDetails();
            a.RoomNum = userToRetrieve.roomNum;
            a.FloorLevel = userToRetrieve.floorLevel;
            a.RoomType = userToRetrieve.roomType;
            a.BedType = userToRetrieve.bedType;
            a.SmokingOrNot = userToRetrieve.smokingOrNot;
            a.MaxOccupancy = userToRetrieve.maxOccupancy;
            a.RoomStatus = userToRetrieve.roomStatus;
            a.RoomRates = userToRetrieve.roomRates;
            a.GuestID = userToRetrieve.guestID;
            return a;
        }

        public IEnumerable<RoomDetails> GetRoomByGuestRequirements(string roomType, string bedType, string smokingOrNot)
        {
            var context = new hmsDBEntities();
            var query = from room in context.roomDetails where room.roomType == roomType && room.bedType == bedType && room.smokingOrNot == smokingOrNot && room.roomStatus == "V" select new RoomDetails()
            {
                RoomNum = room.roomNum
            };
            return query;

        }

        public void UpdateRoom(int roomNum, string roomStatus, string guestID)
        {
            var context = new hmsDBEntities();
            var RoomDetailsToUpdate = context.roomDetails.FirstOrDefault(r => r.roomNum == roomNum);
            RoomDetailsToUpdate.roomStatus = roomStatus;
            RoomDetailsToUpdate.guestID = guestID;
            context.SaveChanges();
        }

        public IEnumerable<RoomDetails> GetAllRoom(string status)
        {
            var context = new hmsDBEntities();
            var OccupiedRoomToRetrieve = from r in context.roomDetails
                                         where r.roomStatus == status
                                         select new RoomDetails()
                                         {
                                             RoomNum = r.roomNum,
                                             FloorLevel = r.floorLevel,
                                             RoomType = r.roomType,
                                             BedType = r.bedType,
                                             SmokingOrNot = r.smokingOrNot,
                                             MaxOccupancy = r.maxOccupancy,
                                             RoomStatus = r.roomStatus,
                                             RoomRates = r.roomRates,
                                             GuestID = r.guestID
                                         };
            return OccupiedRoomToRetrieve;
        }
    }
}
